package ciicos.service.impl;

import ciicos.base.ActionResult;
import ciicos.entity.*;
import ciicos.mapper.LineLineMapper;
import ciicos.model.lineplace.LinePlaceForm;
import ciicos.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ciicos.util.RandomUtil;

import java.math.BigDecimal;

import cn.hutool.core.util.ObjectUtil;
import ciicos.permission.model.authorize.AuthorizeConditionModel;

import ciicos.util.GeneraterSwapUtil;
import ciicos.model.visiual.superQuery.ConditionJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryConditionModel;


import ciicos.permission.service.AuthorizeService;


import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;

import java.util.stream.Collectors;

import ciicos.model.lineline.*;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import ciicos.util.*;

import java.util.*;

import ciicos.base.model.filter.RuleInfo;
import ciicos.permission.util.GenUtil;

/**
 * 线缆管理
 * 版本： V3.2.0
 * 版权：
 * 作者： 企业研发中心
 * 日期： 2025-10-03
 */
@Service
public class LineLineServiceImpl extends ServiceImpl<LineLineMapper, LineLineEntity> implements LineLineService {

    private final static Class eClass = LineLineEntity.class;

    @Autowired
    private LineLinePlaceRelationService lineLinePlaceRelationService;

    @Override
    public List<LineLineEntity> getList(LineLinePagination pagination) {
        String dataType = pagination.getDataType();
        QueryWrapper<LineLineEntity> queryWrapper = new QueryWrapper<>();
        AutoCodeUtil.highQuery(pagination.getSuperQueryJson(),eClass,queryWrapper);
        if (ObjectUtil.isNotEmpty(pagination.getCode())) {
            queryWrapper.lambda().like(LineLineEntity::getCode, pagination.getCode());
        }

        if (ObjectUtil.isNotEmpty(pagination.getName())) {
            queryWrapper.lambda().like(LineLineEntity::getName, pagination.getName());
        }

        if (ObjectUtil.isNotEmpty(pagination.getType())) {
            queryWrapper.lambda().eq(LineLineEntity::getType, pagination.getType());
        }

        if (ObjectUtil.isNotEmpty(pagination.getSelectKey())) {
            queryWrapper.lambda()
                    .like(LineLineEntity::getCode, pagination.getSelectKey())
                    .or()
                    .like(LineLineEntity::getName, pagination.getSelectKey());
        }

        //排序+分页
        return AutoCodeUtil.sortPageList(this,queryWrapper, eClass,
                pagination.getSidx(), pagination.getSort(),
                LineLineEntity::getCreatetime, true,pagination,StringUtil.equals("0",dataType));
    }
    //验证表单唯一字段
    @Override
    public ActionResult checkForm(LineLineForm form, String id) {
        boolean isUp = StringUtil.isNotEmpty(form.getId()) && !form.getId().equals("0");
        if ("0".equals(id)) {//创建
            if (isUp) {
                return ActionResult.fail("创建时不能传id");
            }
        } else {
            if (!StringUtil.equals(id, form.getId())) {
                return ActionResult.fail("更新id校验错误");
            }
        }

        if (ObjectUtil.isEmpty(form.getName())) {
            return ActionResult.fail("名称不能为空");
        }

        if (ObjectUtil.isEmpty(form.getCode())) {
            return ActionResult.fail("编码不能为空");
        }
        QueryWrapper<LineLineEntity> queryWrapper = new QueryWrapper<>();
        if (isUp) {
            queryWrapper.lambda().ne(LineLineEntity::getId, id);
        }
        queryWrapper.lambda().and(t ->
                t.eq(LineLineEntity::getCode, form.getCode()).or().eq(LineLineEntity::getName, form.getName())
        );

        LineLineEntity entity = this.getOne(queryWrapper);
        if (entity == null) {
            return ActionResult.success();
        } else {
            if (StringUtil.equals(form.getName(), entity.getName())) {
                return ActionResult.fail("名称不能重复");
            } else {
                return ActionResult.fail("编码不能重复");
            }
        }
    }

    @Override
    public long getTypeCount() {
        QueryWrapper<LineLineEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct F_TYPE");
        return this.getBaseMapper().selectCount(queryWrapper);
    }

    @Override
    public List<LinePlaceEntity> getPlaceListByLineId(String lineId) {
        return lineLinePlaceRelationService.getPlaceListByLineId(lineId);
    }


}
